package com.google.android.apps.wakemewhere.ui;

import java.util.Timer;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.apps.wakemewhere.Alarm;
import com.google.android.apps.wakemewhere.Alarms;
import com.google.android.apps.wakemewhere.IntentExtras;
import com.google.android.apps.wakemewhere.R;

public class AlarmFireActivity extends Activity {

	private AnimationDrawable backgroundAnimation;
	private Animation foregroundAnimation;
	private Sound sound;
	private Vibrator v;
	private ImageView alarmBackgroundImageView;
	private ImageView droidImageView;
	private TextView titleTextView;
	private TextView summaryTextView;
	private Timer timer = new Timer();
	private Alarm alarm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm);
		Button dismissButton = (Button) findViewById(R.id.dismiss);

		long alarmId = getIntent().getLongExtra(IntentExtras.ALARM_ID, -1L);
		alarm = Alarms.getAlarm(getContentResolver(), alarmId);

		dismissButton.setOnClickListener(new DismissListener());

		titleTextView = (TextView) findViewById(R.id.title);
		summaryTextView = (TextView) findViewById(R.id.summary);
		alarmBackgroundImageView = (ImageView) findViewById(R.id.alarm_background);
		alarmBackgroundImageView
				.setBackgroundResource(R.anim.alarm_background_anim);
		backgroundAnimation = (AnimationDrawable) alarmBackgroundImageView
				.getBackground();

		droidImageView = (ImageView) findViewById(R.id.alarm_droid);
		droidImageView.setImageResource(R.drawable.fg1);

		foregroundAnimation = AnimationUtils.loadAnimation(this,
				R.anim.alarm_droid_anim);
		foregroundAnimation.setRepeatMode(Animation.RESTART);
		foregroundAnimation.setRepeatCount(Animation.INFINITE);

		titleTextView.setText(alarm.getLabel());
		summaryTextView.setText(alarm.getLocationSummary());

		sound = new Sound(getBaseContext());
		sound.play();

		if (alarm.shouldVibrate()) {
			v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

			// Vibrate in a Pattern with 500ms on, 500ms off for 5 times
			long[] pattern = { 0, 200, 500 };
			v.vibrate(pattern, 0);
		}

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
			backgroundAnimation.start();
			droidImageView.startAnimation(foregroundAnimation);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		stop();
	};

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		alarm.setExited(false); // Mark the alarm, to prevent subsequent firing.
		stop();
	}

	private void stop() {
		v.cancel();
		sound.stop();
		backgroundAnimation.stop();
	}

	private class DismissListener implements OnClickListener {
		public void onClick(View view) {
			onBackPressed();
		}
	}

	// TODO(tcox): Make this work on 2.1
	private class Sound /* implements SoundPool.OnLoadCompleteListener */{
		private MediaPlayer mediaPlayer;

		Sound(Context context) {
			mediaPlayer = MediaPlayer.create(context, R.raw.sound);
			mediaPlayer.setLooping(true);
		}

		public void stop() {
			mediaPlayer.stop();
		}

		public void play() {
			mediaPlayer.start();
		}

	}
}